import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/components/Main.vue'

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录',
      },
      component: () => import('@/components/Login.vue')
    }, {
      path: '/pay',
      name: 'pay',
      meta: {
        title: '支付',
      },
      component: () => import('@/components/pay/Pay.vue')
    }, {
      path: '/payok',
      name: 'payok',
      meta: {
        title: '支付成功',
      },
      component: () => import('@/components/pay/PayOk.vue')
    }, {
      path: '/',
      name: 'main',
      redirect: '/index',
      component: Main,
      children: [
        {
          path: 'index',
          meta: {
            title: '系统首页',
          },
          name: 'index',
          component: () => import('@/components/Index.vue')
        }, {
          path: 'buy',
          meta: {
            title: '购买商品',
            parent: 'order',
          },
          name: 'buy',
          component: () => import('@/components/order/Buy.vue')
        },
      ]
    }, {
      path: '/user',
      name: 'user',
      component: Main,
      children: [
        {
          path: 'info',
          meta: {
            title: '用户信息',
            parent: 'user',
          },
          name: 'info',
          component: () => import('@/components/user/Info.vue')
        },
      ]
    }, {
      path: '/order',
      name: 'order',
      component: Main,
      children: [
        {
          path: 'factory',
          meta: {
            title: '厂商列表',
            parent: 'order',
          },
          name: 'factory',
          component: () => import('@/components/order/Factory.vue')
        }, {
          path: 'good',
          meta: {
            title: '商品列表',
            parent: 'order',
          },
          name: 'good',
          component: () => import('@/components/order/Good.vue')
        }, {
          path: 'list',
          meta: {
            title: '订单列表',
            parent: 'order',
          },
          name: 'order-list',
          component: () => import('@/components/order/Order.vue')
        }
      ]
    }, {
      path: '/good',
      name: 'good',
      component: Main,
      children: [
        {
          path: 'list',
          meta: {
            title: '商品列表',
            parent: 'good',
          },
          name: 'good-list',
          component: () => import('@/components/good/Good.vue')
        }, {
          path: 'apply',
          meta: {
            title: '供货列表',
            parent: 'good',
          },
          name: 'apply',
          component: () => import('@/components/good/Apply.vue')
        }
      ]
    }
  ]
});

router.beforeEach(((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  // 判断是否登录
  if (to.name !== 'login' && to.name !== 'pay' && to.name !== 'payok' && !sessionStorage.getItem("token")) {
    next('login');
  } else {
    next();
  }
}));

export default router;
